package com.agilegroup.crm.utils;

import java.util.HashMap;
import java.util.List;
import java.util.TimeZone;

import android.app.Activity;
import android.content.ContentValues;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.agilegroup.crm.database.CalendarDAO;

public class CalendarSync {
	
	private Activity activity;
	private Integer messageId;
	private CalendarDAO calendarDAO;
	private long calendarId;
	
	public CalendarSync(Activity activity,Integer messageId) {
		this.messageId=messageId;
		this.activity=activity;
	}
	
	@SuppressWarnings({ "static-access", "unused" })
	public void createEvents(List<com.agilegroup.crm.model.Calendar> calendars){
		AgileCalendar agileCalendar = new AgileCalendar();
		SessionManager sessionManager = new SessionManager(activity.getApplicationContext());
		calendarDAO = new CalendarDAO(activity);
		HashMap<String, String> user = sessionManager.getUserDetails();
     	String email = user.get(sessionManager.KEY_EMAIL);
     	
		long calId = agileCalendar.getUserCalendar(email, activity);
		if (calId == -1) {
		   agileCalendar.addCalendar("AgileGroupCalendar", "AgileGroupCalendar", email, activity);
		   calendarId = agileCalendar.getUserCalendar(email, activity);
		}
		else{
			calendarId = agileCalendar.getUserCalendar(email, activity);
		}
		
		for (com.agilegroup.crm.model.Calendar calendar : calendars) {
//			if(calendarStatus.size()>0){
				boolean isSynced = calendarDAO.checkCalendarIsSynced(calendar.getId().toString());
				System.out.println("calendar id is>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+calendar.getId().toString());
				if(isSynced==true){
					Log.d(ContactsSync.class.getSimpleName(), "Already synced");
				}
				else{
					long startMillis = calendar.getStartDate().getTime();
					long endMillis = calendar.getEndDate().getTime();
					Log.d("startMillis", String.valueOf(startMillis));
					Log.d("endMillis", String.valueOf(endMillis));
					//insert the event
					ContentValues values = new ContentValues();
					Uri eventsURI = (Uri.parse("content://com.android.calendar/events"));
					values.put("dtstart", startMillis);
					values.put("dtend", endMillis);
					values.put("hasAlarm", true);
	
					int currentapiVersion = android.os.Build.VERSION.SDK_INT;
					if (currentapiVersion>android.os.Build.VERSION_CODES.HONEYCOMB_MR2){
						if(calendar.getEventType().equals("appointment")){
							values.put("eventColor", Color.rgb(226, 109, 0));
						}
						else{
							values.put("eventColor", Color.rgb(134, 168, 0));
						}
					}
					values.put("title", calendar.getTitle());
					values.put("description", calendar.getDescription());
					values.put("calendar_id", calendarId);
					TimeZone timeZone = TimeZone.getDefault();
					values.put("eventTimezone", timeZone.getID());
					
					Uri uri = activity.getContentResolver().
						            insert(eventsURI, values);
					
					long eventId = Long.valueOf(uri.getLastPathSegment());
					System.out.println("eventId>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+eventId);
					Log.d(CalendarSync.class.getSimpleName(), "event is created successfully");
					calendarDAO.createCalendar(calendar.getId().toString(), "true");
					
					ContentValues reminders = new ContentValues();
		            reminders.put("event_id", eventId);
		            reminders.put("method", 0x00000001);
		            
		            reminders.put("minutes", 1);
		            Uri remindersUri = Uri.parse("content://com.android.calendar/reminders");
		            Uri uri2 = activity.getContentResolver().insert(remindersUri, reminders);

			}
		}
		switch (messageId) {
		case 1:
			Toast.makeText(activity.getApplicationContext(), "The Appointments Have successfully synced to the Calendar", Toast.LENGTH_SHORT).show();
			break;
		case 2:
			Toast.makeText(activity.getApplicationContext(), "The Calls Have successfully synced to the Calendar", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}
}